# -*- coding: utf-8 -*-
__author__ = 'Dandelion'
__email__ = 'xushiluo@163.com'
import os
from qgis.core import *
from qgis.utils import iface
from osgeo import gdal

class SetNodataUtils:
    # -------------------------------------------------------------------------------------------------
    #获取所有的栅格图层
    @staticmethod
    def getRasterLayers():
        layers = QgsMapLayerRegistry.instance().mapLayers().values()
        raster = []
        for layer in layers:
            if layer.type() == layer.RasterLayer:
                if layer.providerType() == 'gdal':  # only gdal file-based layers
                    raster.append(layer)
        return raster

    #获取所有矢量图层
    @staticmethod
    def getVectorLayers(shapetype=[-1]):
        layers = QgsMapLayerRegistry.instance().mapLayers().values()
        vector = []
        for layer in layers:
            if layer.type() == layer.VectorLayer:
                if shapetype == [-1] or layer.geometryType() in shapetype:
                    uri = unicode(layer.source())
                    if not uri.lower().endswith('csv') \
                            and not uri.lower().endswith('dbf'):
                        vector.append(layer)
        return vector

    #获取图层扩展的文件名
    @staticmethod
    def getExtendedLayerName(layer):
        authid = layer.crs().authid()
        if authid is not None:
            return layer.name() + ' [' + str(authid) + ']'
        else:
            return layer.name()

    # 根据图层对象获取输入图层的路径
    @staticmethod
    def getInputLayerPath(layerObj):
        lyrPath = None
        if isinstance(layerObj, QgsRasterLayer):
            lyrPath = unicode(layerObj.dataProvider().dataSourceUri())
        elif isinstance(layerObj, QgsVectorLayer):
            lyrPath = unicode(layerObj.source())
        else:
            lyrPath = unicode(layerObj)
            layers = SetNodataUtils.getRasterLayers()
            for layer in layers:
                if layer.name() == lyrPath:
                    lyrPath = unicode(layer.dataProvider().dataSourceUri())
        return lyrPath
     # -------------------------------------------------------------------------------------------------